﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class historyView : System.Web.UI.Page
{
    private String region = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        region = Request.Params["Region"];
        lbl_title.Text = region + " 지역 인구 History";
        getHistory();
    }

    private void getHistory(){
        SqlConnection connection = dbConnection.getConnection(Server.MapPath("~"));
        //구글 시각화 API 클래스 App_code 폴더 참조
        GoogleBizApi api = new GoogleBizApi();
        api.AddColumn("string:년도,number:인구,number:인구밀도");
        string sqlString = "select * from tblPopulation where Region=@region order by Year Asc";
        SqlDataReader dr = null;
        try
        {
            using (connection)
            {
                using (SqlCommand command = new SqlCommand(sqlString, connection))
                {

                    command.Parameters.Add("@region", SqlDbType.VarChar, 50).Value = region;
                    connection.Open();
                    dr = command.ExecuteReader(CommandBehavior.CloseConnection);
                    while (dr.Read())
                    {
                        api.SetCell(dr["Year"].ToString() + "," + dr["Population"].ToString() + "," + dr["Density"].ToString());
                    }
                    string strSource = api.GetSourceView("Table", "display_table", "showRowNumber: false");
                    strSource += api.GetSourceView("ColumnChart", "display_chart", "width: 1000, height: 290, legend: 'bottom', title: '" + region  + " 지역 년도별 인구/인구밀도 [단위 : 인구-천명, 인구밀도-명/㎢]'");

                    //생성된 스크립트 실행
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "ViewData", strSource, false);
                }

            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }
        finally
        {
            if (dr != null)
                dr.Close();
            if (connection != null)
                connection.Close();
        }

    }

}
